//创建用户相关的小仓库
// 导入token方法
import { getToken, setToken, removeToken } from '@/utils/cookie'
// 导入pinia库中的defineStore方法
import { defineStore } from 'pinia'
// 导入tagsView小仓库

// 导入user相关的api
import { register, login, getUserInfo, useTable, userForgot } from '@/api/user'
// 导入路由相关的常量和异步路由

// 导入lodash中的cloneDeep方法

// 导入router和resetRouter
import { encrypt } from '@/utils'
import router from '@/router'
const getDefaultState = () => {
  return {
    token: getToken(), // 用户唯一标识token
    name: null,
    account: null,
    phone: null,
    points: null,
    balance: null,
    userUseTable: []
  }
}
// 创建用户小仓库
const useUserStore = defineStore('User', {
  // 小仓库存储数据地方
  state: () => {
    return getDefaultState()
  },
  // 异步|逻辑的地方
  actions: {
    // 用户登录的方法
    async userLogin(userInfo) {
      const { password, account } = userInfo
      const postUserInfo = { password: await encrypt(password), account }
      // 登录请求
      try {
        const res = await login(postUserInfo)
        if (res.code === 2000) {
          const { data } = res
          this.account = data.account
          this.token = data.token
          console.log(this.account)
          setToken(data.token)
          if (data.account === 'Wcadmin1') {
            router.push('/admin')
          } else {
            router.push('/')
          }

          return {
            code: 2000
          }
        } else {
          return {
            code: 2001
          }
        }
      } catch (error) {
        console.log('error: ', error)
        return Promise.reject(new Error(error))
      }
    },
    // 用户注册方法
    async userRegister(userInfo) {
      const { name, password, account, phone, code } = userInfo
      const postUserInfo = {
        name: name.trim(),
        password: await encrypt(password),
        account: account.trim(),
        phone: phone.trim(),
        code
      }
      // 注册请求
      try {
        const res = await register(postUserInfo)
        if (res.code === 2000) {
          const { data } = res
          this.token = data.token
          setToken(data.token)
          router.push('/')
          return {
            code: 2000
          }
        } else {
          return {
            code: 2001
          }
        }
      } catch (error) {
        console.log('error: ', error)
        return Promise.reject(new Error(error))
      }
    },
    // 用于修改密码方法
    async userForgotPassword(userInfo) {
      const { password, account, phone, code } = userInfo
      const postUserInfo = {
        password: await encrypt(password),
        account: account.trim(),
        phone: phone.trim(),
        code
      }
      try {
        const res = await userForgot(postUserInfo)
        if (res.data.scode === 2) {
          return {
            code: 2000,
            scode: res.data.scode
          }
        } else {
          return {
            code: 2001,
            scode: res.data.scode
          }
        }
      } catch (error) {
        console.log('error: ', error)
        return Promise.reject(new Error(error))
      }
    },
    // 获取用户信息方法
    async userInfo() {
      try {
        const { data } = await getUserInfo()
        const { name, account, phone, points, balance } = data.userInfo
        this.name = name
        this.account = account
        this.phone = phone
        this.points = points
        this.balance = balance
      } catch (error) {
        return Promise.reject(new Error(error))
      }
    },
    // 获取用户当前使用或预约的球桌号
    async getUseruseTable() {
      try {
        const res = await useTable()
        const {
          data: { using }
        } = res
        this.userUseTable = using
      } catch (error) {
        console.log(error)
      }
    },
    // 退出登录
    userLogout() {
      return new Promise((resolve) => {
        this.name = null
        this.account = null
        this.phone = null
        this.points = null
        this.balance = null
        this.userUseTabl = []
        removeToken() // 清除 token
        resolve()
      })
    }
  }
})
// 对外暴露获取小仓库方法
export default useUserStore
